//
//  BaseviewController.swift
//  SWfanghu
//
//  Created by 赵宇桥 on 16/6/6.
//  Copyright © 2016年 ly. All rights reserved.
//

import Foundation
import UIKit

class BaseviewController: UIViewController {
    // MARK: Properties
    
    var frameSize = CGRect(x: 0, y: 0, width: 640, height: 1136)
    var navbarHeight:CGFloat {
        let status = UIApplication.sharedApplication().statusBarFrame.height
        let bar = self.navigationController?.navigationBar.frame.height
        return (status + bar!) / UIScreen.mainScreen().bounds.height * 1136
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = color_weak_background
    }
    
    // MARK: hide keyboard
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { view.endEditing(true) }


    func addButton(frame:CGRect, tittle:String, target:UIViewController, action: Selector)->UIButton{
    
        let button = UIButton()
        
        button.frame = frame
        button.setTitle(tittle, forState: .Normal)
        button.backgroundColor = color_import_red
        button.tintColor = UIColor.whiteColor()
        button.layer.cornerRadius = 5
        //button.titleLabel?.font = UIFont.systemFontOfSize(40)
        button.addTarget(target, action:action, forControlEvents: .TouchUpInside)
        
        return button
    }
    
   
}
public func getStringWidthBy(string:String, font:CGFloat)->CGFloat{
    
    let dic = [NSFontAttributeName : UIFont.systemFontOfSize(font)]
    let size = string.boundingRectWithSize(CGSize(), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: dic, context: nil);
    
    return size.width / UIScreen.mainScreen().bounds.width * 640
    
}



public func CADipRectMake(x: CGFloat, _ y: CGFloat, _ width: CGFloat, _ height: CGFloat) -> CGRect{
    
    let windowSize = UIScreen.mainScreen().bounds
    
    let horizontalScale = CGFloat(windowSize.size.width)/320
    let verticalScale = CGFloat(windowSize.size.height)/568
    
    return CGRect(x:x / 2 * horizontalScale , y:y / 2 * verticalScale,
                  width:width / 2 * horizontalScale, height:height / 2 * verticalScale)
}

extension UILabel{
    convenience init(CADipX:CGFloat, CADipY:CGFloat, string:String, font:CGFloat = 17){
        self.init()
        let dic = [NSFontAttributeName : UIFont.systemFontOfSize(font)]
        let size = string.boundingRectWithSize(CGSize(), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: dic, context: nil);
        
        let CADipW = size.width / UIScreen.mainScreen().bounds.width * 640
        let CADipH = size.height / UIScreen.mainScreen().bounds.height * 1136
        
        self.frame = CADipRectMake(CADipX, CADipY, CADipW, CADipH)
        self.text = string
        self.font = UIFont.systemFontOfSize(font)

    }
    
    func getLabelCADipSize()->CGSize{
        let dic = [NSFontAttributeName : self.font]
        let size = self.text!.boundingRectWithSize(CGSize(), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: dic, context: nil);
        
        let CADipW = size.width / UIScreen.mainScreen().bounds.width * 640
        let CADipH = size.height / UIScreen.mainScreen().bounds.height * 1136

        return CGSize(width: CADipW, height: CADipH)
    }
}
